﻿using OpenDeploy.Domain.Models;

namespace OpenDeploy.Domain;

/// <summary>
/// 解决方案领域服务
/// </summary>
public class SolutionManager
{
    /// <summary>
    /// 根据Git路径发现解决方案
    /// </summary>
    public static List<Solution> DetectSolutions(string gitRepoPath)
    {
        string[] solutionFilePaths = Directory.GetFiles(gitRepoPath, "*.sln", SearchOption.AllDirectories);
        if (solutionFilePaths == null || solutionFilePaths.Length == 0)
        {
            throw new Exception("未找到解决方案");
        }
        solutionFilePaths = solutionFilePaths.Where(a => !a.Contains("ueditor", StringComparison.OrdinalIgnoreCase)).ToArray();

        var solutions = solutionFilePaths.Select(solutionFilePath =>
        {
            var solutionDir = Path.GetDirectoryName(solutionFilePath)!;
            var solutionDisplayName = Path.GetFileNameWithoutExtension(solutionDir);
            var solutionFileName = Path.GetFileNameWithoutExtension(solutionFilePath);
            return new Solution
            {
                Id = Guid.NewGuid(),
                GitRepositoryPath = gitRepoPath,
                SolutionDir = solutionDir,
                SolutionDisplayName = solutionDisplayName,
                SolutionFileName = solutionFileName,
                SolutionFilePath = solutionFilePath,
            };
        }).ToList();

        foreach (var solution in solutions)
        {
            string[] projectFilePaths = Directory.GetFiles(solution.SolutionDir, "*.csproj", SearchOption.AllDirectories);
            foreach (var projectFilePath in projectFilePaths)
            {
                var projectDir = Path.GetDirectoryName(projectFilePath);
                var projectName = Path.GetFileNameWithoutExtension(projectFilePath);
                var webConfigFiles = Directory.GetFiles(projectDir!, "web.config", SearchOption.TopDirectoryOnly);
                var project = new Project
                {
                    Id = Guid.NewGuid(),
                    SolutionId = solution.Id,
                    SolutionName = solution.SolutionFileName,
                    ProjectName = projectName,
                    ProjectFilePath = projectFilePath,
                    ProjectDir = projectDir!,
                    ReleaseDir = string.Empty,
                    IsWeb = webConfigFiles != null && webConfigFiles.Length > 0,
                    IsEnabled = true,
                };
                solution.Projects.Add(project);
            }
        }

        return solutions;
    }


    /// <summary>
    /// 根据Git路径发现解决方案
    /// </summary>
    public static Solution DetectSolution(string gitRepoPath)
    {
        string[] solutionFilePaths = Directory.GetFiles(gitRepoPath, "*.sln", SearchOption.AllDirectories);
        if (solutionFilePaths == null || solutionFilePaths.Length == 0)
        {
            throw new Exception("未找到解决方案");
        }
        solutionFilePaths = solutionFilePaths.Where(a => !a.Contains("ueditor", StringComparison.OrdinalIgnoreCase)).ToArray();

        string[] projectFilePaths = Directory.GetFiles(gitRepoPath, "*.csproj", SearchOption.AllDirectories);
        if (projectFilePaths == null || projectFilePaths.Length == 0)
        {
            throw new Exception("未找到项目");
        }

        var solutionFilePath = solutionFilePaths[0];
        var solutionDir = Path.GetDirectoryName(solutionFilePath);
        var solutionName = Path.GetFileNameWithoutExtension(solutionFilePath);

        var solution = new Solution
        {
            Id = Guid.NewGuid(),
            GitRepositoryPath = gitRepoPath,
            SolutionDir = solutionDir!,
            SolutionFileName = solutionName
        };

        foreach (var projectFilePath in projectFilePaths)
        {
            var projectDir = Path.GetDirectoryName(projectFilePath);
            var projectName = Path.GetFileNameWithoutExtension(projectFilePath);
            var webConfigFiles = Directory.GetFiles(projectDir!, "web.config", SearchOption.TopDirectoryOnly);
            var project = new Project
            {
                Id = Guid.NewGuid(),
                SolutionId = solution.Id,
                SolutionName = solution.SolutionFileName,
                ProjectName = projectName,
                ProjectDir = projectDir!,
                ReleaseDir = string.Empty,
                IsWeb = webConfigFiles != null && webConfigFiles.Length > 0,
                IsEnabled = true,
            };
            solution.Projects.Add(project);
        }
        return solution;
    }
}
